package com.product.easyExcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.product.utile.StringUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

/**
 * @description: EasyExcel
 * @date: 2021/3/26
 * @author: 王瑾
 */
public class EasyExcelUtil {

    /**
     * 写
     * @param filePath 文件路径
     * @param t 模板类
     * @throws Exception
     */
    public static void writeExcel(String filePath, List<?> t) throws Exception{
        ExcelWriter excelWriter = null;

        if(StringUtil.isEmpty(filePath)){
            throw new Exception("传入文件路径为空");
        }
        if(t==null && t.size()==0){
            throw new Exception("传入数据为空");
        }
        try {
            excelWriter = EasyExcel.write(filePath, t.get(0).getClass()).registerConverter(new LocalDateTimeConverter()).build();
            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
            excelWriter.write(t, writeSheet);
        } catch (Exception e){
            e.printStackTrace();

        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    /**
     * 读
     * @param files 文件
     * @param t 模板类
     */
    public static void readExcel(MultipartFile files, Class<?> t) throws IOException {
        InputStream ins = null;
        ins = files.getInputStream();
        File file = new File(files.getOriginalFilename());
        inputStreamToFile(ins, file);
        ins.close();
        ExcelReader excelReader = EasyExcel.read(file, t, new ExcelListener()).registerConverter(new LocalDateTimeConverter()).build();
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        excelReader.read(readSheet);
        excelReader.finish();
        delteTempFile(file);
    }
    /**
     * 读
     * @param file 文件
     * @param t 模板类
     */
    public static void readExcel(File file, Class<?> t) {
        ExcelReader excelReader = EasyExcel.read(file, t, new ExcelListener()).registerConverter(new LocalDateTimeConverter()).build();
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        excelReader.read(readSheet);
        excelReader.finish();
        delteTempFile(file);
    }

    /**
     * 删除本地临时文件
     * @param file
     */
    public static void delteTempFile(File file) {
        if (file != null) {
            File del = new File(file.toURI());
            del.delete();
        }
    }
    /**
     * 获取流文件
     * @param ins
     * @param file
     */
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 读
     * @param filePath 文件路径
     * @param t 模板类
     */
    public static void readExcel(String filePath, Class<?> t){
        ExcelReader excelReader = EasyExcel.read(filePath, t, new ExcelListener()).registerConverter(new LocalDateTimeConverter()).build();
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        excelReader.read(readSheet);
        excelReader.finish();
    }


    public static void main(String[] args) {
        String fileName = "E:\\测试文件夹\\aaaa.xls";
//        File file = new File(fileName);
//        readExcel(file,MustRisk.class);
    }
}
